/**
 * 休假申请模块
 */

const router = require("koa-router")();
const Leave = require("../models/leaveSchema");
const Dept = require("../models/deptSchema");
const util = require("../utils/util");
router.prefix("/leave");

//查询申请列表
router.get("/list", async (ctx) => {
  const { applyState, type } = ctx.request.query;
  const { page, skipIndex } = util.paper(ctx.request.query);
  let authorization = ctx.request.headers.authorization;
  let { data } = util.decoded(authorization);

  try {
    let params = {};
    if (type == "approve") {
      if (applyState == 1 || applyState == 2) {
        params.curAuditUserName = data.userName;
        params.$or = [{ applyState: 1 }, { applyState: 2 }];
      } else if (applyState > 2) {
        params = { "auditFlows.userId": data.userId, applyState };
      } else {
        params = { "auditFlows.userId": data.userId };
      }
    } else {
      params = {
        "applyUser.userId": data.userId,
      };
      if (applyState) params.applyState = applyState;
    }

    const query = Leave.find(params);
    const list = await query.skip(skipIndex).limit(page.pageSize);
    const total = await Leave.countDocuments(params);
    ctx.body = util.success({
      page: {
        ...page,
        total,
      },
      list,
    });
  } catch (error) {
    ctx.body = util.fail(`查询异常：${error.stack}`);
  }
});

router.get("/count", async (ctx) => {
  let authorization = ctx.request.headers.authorization;
  let { data } = util.decoded(authorization);
  try {
    let params = {};
    params.curAuditUserName = data.userName;
    params.$or = [{ applyState: 1 }, { applyState: 2 }];
    const total = await Leave.countDocuments(params);
    ctx.body = util.success(total, "查询成功");
  } catch (error) {
    ctx.body = util.fail(`查询异常：${error.message}`);
  }
});

router.post("/operate", async (ctx) => {
  const { _id, action, ...params } = ctx.request.body;
  let authorization = ctx.request.headers.authorization;
  let { data } = util.decoded(authorization);

  if (action == "add") {
    //生成申请单号
    let orderNo = "XJ";
    orderNo += util.formateDate(new Date(), "yyMMdd");
    const total = await Leave.countDocuments();
    params.orderNo = orderNo + total;
    //获取用户上级部门负责人信息
    let id = data.deptId.pop();
    //查找负责人信息
    let dept = await Dept.findById(id);
    let userList = await Dept.find({
      deptName: { $in: ["人事部", "财务部"] },
    });
    //定义完整审批人
    let auditUsers = dept.userName;
    //审批流创建
    let auditFlows = [
      {
        userId: dept.userId,
        userName: dept.userName,
        userEmail: dept.userEmail,
      },
    ];
    userList.map((item) => {
      auditFlows.push({
        userId: item.userId,
        userName: item.userName,
        userEmail: item.userEmail,
      });
      auditUsers += "," + item.userName;
    });
    params.auditUsers = auditUsers;
    params.curAuditUserName = dept.userName; //当前审批人
    params.auditFlows = auditFlows;
    params.auditLogs = [];
    params.applyUser = {
      //当前申请用户
      userId: data.userId,
      userName: data.userName,
      userEmail: data.userEmail,
    };
    await Leave.create(params);
    ctx.body = util.success("", "创建成功");
  } else if (action == "del") {
    await Leave.findByIdAndUpdate(_id, {
      applyState: 5,
      updateTime: new Date(),
    });

    ctx.body = util.success("", "删除成功");
  }
});

router.post("/approve", async (ctx) => {
  const { action, remark, _id } = ctx.request.body;
  let authorization = ctx.request.headers.authorization;
  let { data } = util.decoded(authorization);
  try {
    let doc = await Leave.findById(_id);
    let auditLogs = doc.auditLogs || [];
    let parmas = {};
    //审批状态  1: "待审批",2: "审批中",3: "审批拒绝",4: "审批通过
    if (action == "refuse") {
      //审核拒绝
      parmas.applyState = 3;
    } else {
      //审核通过
      if (doc.auditFlows.length == doc.auditLogs.length) {
        ctx.body = util.success("当前申请已处理，请勿重复提交");
        return;
      } else if (doc.auditFlows.length == doc.auditLogs.length + 1) {
        parmas.applyState = 4;
      } else if (doc.auditFlows.length > doc.auditLogs.length) {
        parmas.applyState = 2;
        parmas.curAuditUserName =
          doc.auditFlows[doc.auditLogs.length + 1].userName;
      }
    }
    auditLogs.push({
      userId: data.userId,
      userName: data.userName,
      createTime: new Date(),
      remark,
      action: action == "refuse" ? "审核拒绝" : "审核通过",
    });
    parmas.auditLogs = auditLogs;
    await Leave.findByIdAndUpdate(_id, parmas);
    ctx.body = util.success("", "处理成功");
  } catch (error) {
    ctx.body = util.fail(`查询异常:${error.message}`);
  }
});
module.exports = router;
